package main;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class RightSideView {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(8);
        TreeNode left1 = new TreeNode(6);
        TreeNode right1 = new TreeNode(10);
        root.left = left1;
        root.right = right1;
        TreeNode left2 = new TreeNode(5);
        TreeNode right2 = new TreeNode(7);
        left1.left = left2;
        right1.right = right2;
        System.out.println(new RightSideView().rightSideView(root));
    }
    public List<Integer> rightSideView(TreeNode root){
        List<Integer> view = new LinkedList<>();
        if (root == null){
            return view;
        }
        Queue<TreeNode> queue1 = new LinkedList<>();
        Queue<TreeNode> queue2 = new LinkedList<>();
        queue1.offer(root);
        while (!queue1.isEmpty()){
            TreeNode node = queue1.poll();
            if (node.left != null){
                queue2.offer(node.left);
            }
            if (node.right != null){
                queue2.offer(node.right);
            }
            if (queue1.isEmpty()){
                view.add(node.val);
                queue1 = queue2;
                queue2 = new LinkedList<>();
            }
        }
        return view;
    }
}
